package com.net.dao.impl;

import java.util.List;
import org.springframework.stereotype.Component;

import com.net.dao.SuperDao;
import com.net.dao.CategoryDao;
import com.net.model.Category;
@Component("categoryDao")
public class CategoryDaoImpl extends SuperDao implements CategoryDao{

	public boolean delete(Category u) {
		Category cust = (Category) this.getHibernateTemplate().load(Category.class, u.getId());
		if(cust!=null){
			this.getHibernateTemplate().delete(cust);
			return true;
		}else
			return false;
	}

	@SuppressWarnings("unchecked")
	public List<Category> getCategories() {
		return (List<Category>)this.getHibernateTemplate().find("from Category");
	}

	public Category loadCategoryById(int id) {
		return (Category) this.getHibernateTemplate().load(Category.class, id);
	}

	@SuppressWarnings("unchecked")
	public boolean loadCategoryByName(String name) {
		List<Category> categories = getHibernateTemplate().find("from Category c where c.category_name='"+name+"'");
		if(categories!=null&&categories.size()>0){
			return true;
		}else
			return false;
	}

	public void save(Category u) {
		this.getHibernateTemplate().save(u);
	}

	public void update(Category category) {
		this.getHibernateTemplate().update(category);
	}

	public List<Category> searchList(String param1, String param2, String param3) {
		String hql = "";
		if(!param1.equals("")&&!param2.equals("")){
			if(!param3.equals("")){
			String[] str = param3.split("\\.");
			if(str.length==2){
				hql = "from Category c where c."+param1+" like '%"+param2+"%' order by c."+ str[0] + " " + str[1];
				System.out.println(hql);
			}else{
				hql = "from Category c where c."+param1+" like '%"+param2+"%' order by c."+ str[0];
				System.out.println(hql);
			}
			}else{
				hql = "from Category c where c."+param1+" like '%"+param2+"%'";
			}
			return (List<Category>)this.getHibernateTemplate().find(hql);
		}
		return null;
	}

}
